package
{
	import Interfaces.ModuleCommonUI.MISocialUIController;

	public class ParseServerRes
	{
		private static var _instance : ParseServerRes;
		public var _Serverkeys : Object;
		public function ParseServerRes()
		{
			_Serverkeys = new Object();
			_Serverkeys["userGold"] = ShareConstant.RES_GOLD;
			_Serverkeys["userCoin"] = ShareConstant.RES_COIN;
			_Serverkeys["userWood"] = ShareConstant.RES_WOOD;
			_Serverkeys["userIron"] = ShareConstant.RES_IRON;
			_Serverkeys["userStone"] = ShareConstant.RES_STONE;
			_Serverkeys["userHonour"] = ShareConstant.RES_EXP;
			_Serverkeys["userCiv"] = ShareConstant.RES_CIV;
			_Serverkeys["userFanfare"] = ShareConstant.RES_FANFARE;
			_Serverkeys["userResearch"] = ShareConstant.RES_MIL_RESEARCH;
			_Serverkeys["userSun"] = ShareConstant.RES_SUN_STONE;
			_Serverkeys["userGlory"] = ShareConstant.RES_GLORY;
			_Serverkeys["energy"] = ShareConstant.RES_ENERGY;
			_Serverkeys["userETPoint"] = ShareConstant.RES_EVENT_SCORE;
			_Serverkeys["userFPoint"] = ShareConstant.RES_FRIENDSHIP;
			_Serverkeys["userXJC"] = ShareConstant.RES_XJC;
			_Serverkeys["userESPoint"] = ShareConstant.RES_EVENT_EGGP;
		}
		
		public  static function initIntance():void
		{
			if( !_instance )
				_instance = new ParseServerRes();
		}
		
		public static function parseData ( serverData : Object ): Object
		{
			var clientData : Object = new Object();
			var sKey : String = "";
			initIntance();
			for(sKey in serverData)
			{
				if( serverData[sKey] > 0  && _instance.isCommonRes(sKey) )// make sure res > 0
					clientData[ _instance._Serverkeys[sKey] ] = serverData[sKey];
			}
			return clientData;
		}
		
		public static function parseItemCollection(  serverData : Object ):Object
		{
			var clientData : Object = new Object();
			var sKey : String = "";
			var subKey : String = "";
			initIntance();
			for(sKey in serverData)
			{
				subKey = sKey.substring(0,3);
				if( serverData[sKey] > 0 && subKey === "IQ_" )// make sure res > 0 && is item collection
				{
					clientData[sKey.substring(3, sKey.length) ] =  serverData[sKey];
				}
				
			}
			return clientData;
		}
		
		public function isCommonRes( key : String):Boolean
		{
			return _Serverkeys.hasOwnProperty(key);
		}
		
		public  static function getItemsType( servertype : String ):String
		{
			initIntance();
			if( _instance._Serverkeys.hasOwnProperty( servertype ) )
				return _instance._Serverkeys[servertype];
			else
				return ShareConstant.NULL_STRING;
		}
		
		public static function parseCompensation( compServerData : Object, socialCtrl : MISocialUIController, isnew : Boolean = false ):Array
		{
			var compeClient : Object = null;
			var awardDataArr : Array = new Array();
			var element : Object  = null;
			var imageId : int = 0;
			for each( var award : Object in  compServerData)
			{
				if( award.hasOwnProperty("auto") && award["auto"] == 1)
					continue;
				
				element = new Object();
				element.tooltip = award.message;
				element.id = award.id;
				if( isnew )
					element.eff = "arrow";
				
				if( award.hasOwnProperty("image") && !isNaN(parseInt(award.image)) )
				{
					imageId = parseInt(award.image);
					element.image = imageId >= 0 && imageId <= 1? imageId:0 ;
				}
				else
					element.image = 0;
				
				if(award.type == 1)// resource
				{
					award = award["params"][0];
					compeClient  = ParseServerRes.parseData( award);	
				}
				else if(award.type == 5)// building
				{
					var mapId : String = award["params"][0];	// 0 : mapId
					if(mapId != "-1" && mapId != socialCtrl.getCurMapName() )
						continue;
					
					var buildingType : String = award["params"][1]; //	1 : building type
					var quantity : int = 1;
					var lv : int = 1;
					var buildings : Object = new Object();
					if( award["params"].hasOwnProperty("2") )
						quantity = award["params"]["2"];		//  2: quantity 
					if( award["params"].hasOwnProperty("3") )
						lv = award["params"]["3"];				//  3 : level 		
					buildings[buildingType] = {quantity : quantity, lv : lv};
					compeClient = { building : buildings }
				}
				else if( award.type == 6 )// unlock soldier
				{
					compeClient = {unlock : award["params"][0] }
				}
				else if( award.type == 7 )// item quantity
				{
					var itemQuantity : Object = new Object();
					var itemQKey : String = award["params"][0];
					var itemQQuantity : String = award["params"][1];
					itemQuantity[itemQKey] = itemQQuantity;
					compeClient = {item : itemQuantity }
				}
				element.award = compeClient;
				awardDataArr.push( element );
			}
			return awardDataArr;
		}
		
		public static function getAwardHelpFriend( helpData : Object ):Object
		{
			var awardServer: Object = {};
			awardServer["resources"] = {};
			awardServer["resources"][ShareConstant.RES_FRIENDSHIP] = 0;
			awardServer["resources"][ShareConstant.RES_GOLD] = 0
			awardServer["resources"][ShareConstant.RES_SUN_STONE]= 0;
			awardServer["resources"][ShareConstant.RES_ENERGY] = 0;
			
			for(var i: String in helpData)
			{
				// resource
				if(helpData[i].hasOwnProperty("resources"))
				{
					if(helpData[i]["resources"]["incResources"].hasOwnProperty("userGold"))
						awardServer["resources"][ShareConstant.RES_GOLD] = helpData[i]["resources"]["incResources"]["userGold"];
					if(helpData[i]["resources"]["incResources"].hasOwnProperty("userFPoint"))
						awardServer["resources"][ShareConstant.RES_FRIENDSHIP] = helpData[i]["resources"]["incResources"]["userFPoint"];
					if(helpData[i]["resources"]["incResources"].hasOwnProperty("userSun"))
						awardServer["resources"][ShareConstant.RES_SUN_STONE] = helpData[i]["resources"]["incResources"]["userSun"];
					if(helpData[i]["resources"]["incResources"].hasOwnProperty("userResearch"))
						awardServer["resources"][ShareConstant.RES_MIL_RESEARCH] = helpData[i]["resources"]["incResources"]["userResearch"];
					if(helpData[i]["resources"]["incResources"].hasOwnProperty("userHonour"))
						awardServer["resources"][ShareConstant.RES_EXP] = helpData[i]["resources"]["incResources"]["userHonour"];
					if(helpData[i].hasOwnProperty("energy"))
						awardServer["resources"][ShareConstant.RES_ENERGY]= helpData[i]["energy"];
				}
				// item
				if(helpData[i].hasOwnProperty("item_quantities"))
					awardServer["items"] = helpData[i]["item_quantities"];
				
			}// end for
			return awardServer;
		}// end func
		
		public static function getAwardDailyQuest( data : Object ):Object
		{
			var awardServer: Object = {};
			awardServer["resources"] = {};
			awardServer["resources"][ShareConstant.RES_XJC] = 0;			
			// resource
			if(data.hasOwnProperty("resources"))
			{
				if(data["resources"]["incResources"].hasOwnProperty("userXJC"))
					awardServer["resources"][ShareConstant.RES_XJC] = data["resources"]["incResources"]["userXJC"];
			}
			// item
			if(data.hasOwnProperty("item_quantities"))
				awardServer["items"] = data["item_quantities"];				
		
			return awardServer;
		}// end func
		
		public static function getBoostAwardFriend( boostAward : Object ):Object
		{
			var awardServer: Object = new Object();
			for(var i: String in boostAward)
			{
				// item
				if(boostAward[i].hasOwnProperty("item_quantities"))
				{
					for( var itemType : String in boostAward[i].item_quantities )
					{
						if( awardServer.hasOwnProperty( itemType ) )
							awardServer[itemType] += boostAward[i].item_quantities.itemType;
						else
							awardServer[itemType] = boostAward[i].item_quantities[itemType];
					}
				}
				
			}// end for
			return awardServer;
		}// end func
	}
}